﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YLW.Plat.Jhfw.HL7V3.XSDModels.PRPA_IN900350UV_Zyjz;
using YLW.Plat.Jhfw.Model.JhfwZydj;

namespace YLW.Plat.Jhfw.HL7V3.V3Message
{
    public class V3_PRPA_IN900350UV_Zyjz
    {
        public static string InPatientInfoQuery_Response(GetJhfwZydjListOut jsonData, string id_extension, string sender_name, string receiver_name)
        {
            string hl7Xml = string.Empty;

            if (jsonData == null)
            {
                return hl7Xml;
            }

            //根据XML XSD构建HL7 XML消息格式

            //创建根对象
            var xmlRoot = new PRPA_IN900350UV
            {
                ITSVersion = "XML_1.0",
                id = new id { root = "2.16.156.10011.2.5.1.1", extension = Guid.NewGuid().ToString() },
                creationTime = new creationTime { value = DateTime.Now.ToString("yyyyMMddHHmmss") },
                interactionId = new interactionId { root = "2.16.156.10011.2.5.1.2", extension = "PRPA_IN900350UV" },
                processingCode = new processingCode { code = "P" },
                processingModeCode = new processingModeCode(),
                acceptAckCode = new acceptAckCode { code = "AL" },
                receiver = new receiver
                {
                    typeCode = "RCV",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id = new id
                        {
                            root = "2.16.156.10011.2.5.1.3",
                            extension = receiver_name
                        }
                    }
                },
                sender = new sender
                {
                    typeCode = "SND",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id = new id
                        {
                            root = "2.16.156.10011.2.5.1.3",
                            extension = sender_name
                        }
                    }
                },
                acknowledgement = new acknowledgement
                {
                    typeCode = jsonData.subcode == "success" ? "AA" : "AE",
                    targetMessage = new targetMessage
                    {
                        id = new id
                        {
                            root = "2.16.156.10011.2.5.1.1",
                            extension = id_extension
                        }
                    },
                    acknowledgementDetail = new acknowledgementDetail
                    {
                        text = new text
                        {
                            value = jsonData.submsg
                        }
                    }
                },
                controlActProcess = new controlActProcess
                {
                    classCode = "ACTN",
                    moodCode = "PRMS",

                    queryAck = new queryAck
                    {
                        queryId = new queryId { extension = "" },
                        queryResponseCode = new queryResponseCode { code = jsonData.subcode == "success" ? "OK" : "error" },
                    }
                },
            };

            List<subject> subList = new List<subject>();

            if (jsonData != null && jsonData.data != null)
            {
                xmlRoot.controlActProcess.queryAck.resultTotalQuantity = new resultTotalQuantity { value = jsonData.data.Count.ToString() };

                foreach (var listItem in jsonData.data)
                {
                    subList.Add(new subject
                    {
                        typeCode = "SUBJ",
                        encounterEvent = new encounterEvent
                        {
                            classCode = "ENC",
                            moodCode = "EVN",
                            id = new id
                            {
                                item = new item[] {
                                    new item{
                                        root = "2.16.156.10011.1.11",
                                        extension = ""
                                    },
                                    new item{
                                        root = "2.16.156.10011.2.5.1.8",
                                        extension = listItem.series
                                    },
                                    new item{
                                        root = "2.16.156.10011.2.5.1.9",
                                        extension = listItem.jzlsh
                                    }
                                }
                            },
                            code = new code
                            {
                                code1 = "3",
                                codeSystem = "2.16.156.10011.2.3.1.271",
                                codeSystemName = "患者类型代码表",
                                displayName = new displayName { value = "住院" }
                            },
                            statusCode = new statusCode(),
                            effectiveTime = new effectiveTime
                            {
                                low = new low { value = listItem.zyrq }
                            },
                            subject = new subject
                            {
                                typeCode = "SBJ",
                                patient = new patient
                                {
                                    classCode = "PAT",
                                    id = new id
                                    {
                                        item = new item[]
                                        {
                                            new item
                                            {
                                                root = "2.16.156.10011.2.5.1.4",
                                                extension = listItem.mrn
                                            }
                                        }
                                    },
                                    patientPerson = new patientPerson
                                    {
                                        id = new id
                                        {
                                            item = new item[]
                                            {
                                                new item
                                                {
                                                    root = "2.16.156.10011.1.3",
                                                    extension = listItem.pcid
                                                }
                                            }
                                        },
                                        name = new name
                                        {
                                            item = new item
                                            {
                                                part = new part
                                                {
                                                    value = listItem.pname
                                                }
                                            }
                                        }
                                    }
                                }
                            },
                            admitter = new admitter
                            {
                                typeCode = "ADM",
                                time = new time(),
                                assignedPerson = new assignedPerson
                                {
                                    classCode = "ASSIGNED",
                                    id = new id
                                    {
                                        item = new item[] {
                                            new item
                                            {
                                                root = "2.16.156.10011.1.4",
                                                extension = listItem.in_empid
                                            }
                                        }
                                    },
                                    assignedPerson1 = new assignedPerson
                                    {
                                        classCode = "PSN",
                                        determinerCode = "INSTANCE",
                                        name = new name
                                        {
                                            item = new item
                                            {

                                            }
                                        }
                                    }
                                }
                            },
                            location = new location
                            {
                                typeCode = "LOC",
                                time = new time(),
                                statusCode = new statusCode
                                {
                                    code = "active"
                                },
                                serviceDeliveryLocation = new serviceDeliveryLocation
                                {
                                    classCode = "SDLOC",
                                    location = new location
                                    {
                                        classCode = "PLC",
                                        determinerCode = "INSTANCE",
                                        id = new id
                                        {
                                            item = new item[]
                                            {
                                                new item
                                                {
                                                    root = "2.16.156.10011.1.26",
                                                    extension = listItem.zyksdm
                                                }
                                            }
                                        },
                                        name = new name
                                        {
                                            item = new item
                                            {
                                                part = new part
                                                {
                                                    value = listItem.zyksmc
                                                }
                                            }
                                        },
                                        locatedEntityHasParts = new locatedEntityHasParts
                                        {
                                            locatedPlace = new locatedPlace[]
                                            {
                                                new locatedPlace{
                                                    Items=new object[]{
                                                        new id
                                                        {
                                                            root = "2.16.156.10011.1.27",
                                                            extension = listItem.zybqdm
                                                        },
                                                        new name{
                                                            item= new item
                                                            {
                                                                part = new part
                                                                {
                                                                    value = listItem.zybqmc
                                                                }
                                                            }
                                                        },
                                                        new locatedEntityHasParts
                                                        {
                                                            locatedPlace = new locatedPlace[]
                                                            {
                                                                new locatedPlace{
                                                                    Items=new object[]{
                                                                        new id
                                                                        {
                                                                            root = "2.16.156.10011.1.21",
                                                                            extension = listItem.bedid
                                                                        },
                                                                        new name{
                                                                            item= new item
                                                                            {
                                                                                part = new part
                                                                                {
                                                                                    value = listItem.bedid
                                                                                }
                                                                            }
                                                                        },
                                                                        new locatedEntityHasParts
                                                                        {
                                                                            locatedPlace = new locatedPlace[]
                                                                            {
                                                                                new locatedPlace{
                                                                                    Items=new object[]{
                                                                                        new id
                                                                                        {
                                                                                            root = "2.16.156.10011.1.22",
                                                                                            extension = listItem.bfh
                                                                                        },
                                                                                        new name{
                                                                                            item= new item
                                                                                            {
                                                                                                part = new part
                                                                                                {
                                                                                                    value = listItem.bfh
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    },
                                    serviceProviderOrganization = new serviceProviderOrganization
                                    {
                                        classCode = "ORG",
                                        determinerCode = "INSTANCE",
                                        id = new id
                                        {
                                            item = new item[]
                                            {
                                                new item
                                                {
                                                    root = "2.16.156.10011.1.5",
                                                    extension = ""
                                                }
                                            }
                                        }
                                    }
                                }
                            },
                            reason = new reason[] { },
                            extension = new extension
                            {
                                sjly = new sjly { value = new value { Text = new string[] { listItem.sjly } } },
                                lyxt = new lyxt { value = new value { Text = new string[] { listItem.lyxt } } },
                                hoscode = new hoscode { value = new value { Text = new string[] { listItem.hoscode } } },
                                jzlsh = new jzlsh { value = new value { Text = new string[] { listItem.jzlsh } } },
                                mrn = new mrn { value = new value { Text = new string[] { listItem.mrn } } },
                                pname = new pname { value = new value { Text = new string[] { listItem.pname } } },
                                pcid = new pcid { value = new value { Text = new string[] { listItem.pcid } } },
                                series = new series { value = new value { Text = new string[] { listItem.series } } },
                                service = new service { value = new value { Text = new string[] { listItem.service } } },
                                stay = new stay { value = new value { Text = new string[] { listItem.stay } } },
                                zycs = new zycs { value = new value { Text = new string[] { listItem.zycs } } },
                                zyksdm = new zyksdm { value = new value { Text = new string[] { listItem.zyksdm } } },
                                zyksmc = new zyksmc { value = new value { Text = new string[] { listItem.zyksmc } } },
                                zybqdm = new zybqdm { value = new value { Text = new string[] { listItem.zybqdm } } },
                                zybqmc = new zybqmc { value = new value { Text = new string[] { listItem.zybqmc } } },
                                bedid = new bedid { value = new value { Text = new string[] { listItem.bedid } } },
                                bfh = new bfh { value = new value { Text = new string[] { listItem.bfh } } },
                                zyysdm = new zyysdm { value = new value { Text = new string[] { listItem.zyysdm } } },
                                zyysxm = new zyysxm { value = new value { Text = new string[] { listItem.zyysxm } } },
                                zyrq = new zyrq { value = new value { Text = new string[] { listItem.zyrq } } },
                                diagcode = new diagcode { value = new value { Text = new string[] { listItem.diagcode } } },
                                diagname = new diagname { value = new value { Text = new string[] { listItem.diagname } } },
                                zd_empid = new zd_empid { value = new value { Text = new string[] { listItem.zd_empid } } },
                                jzyy = new jzyy { value = new value { Text = new string[] { listItem.jzyy } } },
                                useflag = new useflag { value = new value { Text = new string[] { listItem.useflag } } },
                                in_empid = new in_empid { value = new value { Text = new string[] { listItem.in_empid } } },
                                in_date = new in_date { value = new value { Text = new string[] { listItem.in_date } } },
                                xg_empid = new xg_empid { value = new value { Text = new string[] { listItem.xg_empid } } },
                                xgsj = new xgsj { value = new value { Text = new string[] { listItem.xgsj } } }
                            }
                        }
                    });
                }
            }

            xmlRoot.controlActProcess.subject = subList.ToArray();

            hl7Xml = HL7V3Helper.SerializeToXml(xmlRoot);

            return hl7Xml;
        }
    }
}
